//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.security.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qst.crop.security.entity.AuthenticationBean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    public CustomAuthenticationFilter() {
    }

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (!request.getContentType().equals("application/json;charset=UTF-8") && !request.getContentType().equals("application/json")) {
            return super.attemptAuthentication(request, response);
        } else {
            ObjectMapper mapper = new ObjectMapper();
            UsernamePasswordAuthenticationToken authRequest = null;

            try {
                InputStream is = request.getInputStream();

                try {
                    AuthenticationBean authenticationBean = (AuthenticationBean)mapper.readValue(is, AuthenticationBean.class);
                    authRequest = new UsernamePasswordAuthenticationToken(authenticationBean.getUsername(), authenticationBean.getPassword());
                } catch (Throwable var14) {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (Throwable var13) {
                            var14.addSuppressed(var13);
                        }
                    }

                    throw var14;
                }

                if (is != null) {
                    is.close();
                }
            } catch (IOException var15) {
                var15.printStackTrace();
                authRequest = new UsernamePasswordAuthenticationToken("", "");
            } finally {
                this.setDetails(request, authRequest);
                return this.getAuthenticationManager().authenticate(authRequest);
            }
        }
    }
}
